<template>
  <!-- 工厂信息 -->
  <div>
    <curd-title v-if="infoFlg" :required="!['view'].includes(this.type)" :title="'工厂信息'" />
    <avue-crud
      :table-loading="factoryloading"
      :option="factoryOption"
      :data="factoryList"
      @on-load="onFactoryLoad"
      @row-save="rowFactorySave"
      @row-update="rowFactoryUpdate"
      @refresh-change="refreshFactoryChange"
      @row-del="rowFactoryDel"
      :row-class-name="delRowClassName"
    >
      <template slot="factoryName" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.factoryNameFlag === '1'">{{ scope.row.factoryName }}</del>
          {{ scope.row.factoryNameA }}
        </div>
        <span v-else>{{ scope.row.factoryName }}</span>
      </template>
      <template slot="companyName" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.companyNameFlag === '1'">{{ scope.row.companyName }}</del>
          {{ scope.row.companyNameA }}
        </div>
        <span v-else>{{ scope.row.companyName }}</span>
      </template>
      <template slot="factoryAddress" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.factoryAddressFlag === '1'">{{ scope.row.factoryAddress }}</del>
          {{ scope.row.factoryAddressA }}
        </div>
        <span v-else>{{ scope.row.factoryAddress }}</span>
      </template>
      <template slot="factoryArea" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.factoryAreaFlag === '1'">{{ scope.row.factoryArea }}</del>
          {{ scope.row.factoryAreaA }}
        </div>
        <span v-else>{{ scope.row.factoryArea }}</span>
      </template>
      <template slot="productionCapacity" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.productionCapacityFlag === '1'">{{ scope.row.productionCapacity }}</del>
          {{ scope.row.productionCapacityA }}
        </div>
        <span v-else>{{ scope.row.productionCapacity }}</span>
      </template>
      <template slot="productivity" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.productivityFlag === '1'">{{ scope.row.productivity }}</del>
          {{ scope.row.productivityA }}
        </div>
        <span v-else>{{ scope.row.productivity }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { factoryOption } from '@/option/tenant/supplier/factory-info'
import { getStore } from '@/util/store'
import { getFactoryList, removeFactory, addFactory, updateFactory } from '@/api/supplier/factory'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'FactoryInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      factoryList: [],
      factoryloading: false,
      factoryOption: factoryOption.call(this)
    }
  },
  methods: {
    onFactoryLoad() {
      this.infoFlg
        ? ((this.factoryloading = true),
          getFactoryList({ supplierId: this.supplierId }).then(res => {
            this.factoryList = res.data.data
            this.factoryloading = false
          }))
        : ''
    },
    refreshFactoryChange() {
      this.onFactoryLoad()
    },
    rowFactorySave(row, done, loading) {
      addFactory({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowFactoryUpdate(row, index, done, loading) {
      updateFactory(row).then(
        () => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowFactoryDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeFactory(row.id)
        })
        .then(() => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
